package test04;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.dozer.DozerBeanMapper;

/**
 * 基本类型：双方属性不同类型且不同名称
 * @author Administrator
 *
 */
public class DozerTest04 {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {
		
		/**
		 * PO和VO的属性都是基本类型，但对应属性的名称和属性类型都不相同：必须得配置XML
		 */
		DozerBeanMapper mapper = new DozerBeanMapper();
		
		List mf = new ArrayList();
		mf.add("test04/test04.xml");//格式：“目录/XML文件名”，若无目录时默认为ClassPath目录
		mapper.setMappingFiles(mf);

		//示例一：UserPO -> UserVO
		UserPO po1 = new UserPO();
		po1.setId(100);
		po1.setName("123");
		po1.setAge(10);
		po1.setHeight(170.5F);
		po1.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2008-08-08"));
		
		UserVO vo1 = mapper.map(po1, UserVO.class);

		System.out.println("示例一：由PO向VO转换");
		System.out.println("UserPO	->	UserVO");
		System.out.println(po1.getId()+"[int]    "+vo1.getId2()+"[Integer]");
		System.out.println(po1.getName()+"[String]    "+vo1.getName2()+"[int]");
		System.out.println(po1.getAge()+"[Integer]    "+vo1.getAge2()+"[String]");
		System.out.println(po1.getHeight()+"[float]    "+vo1.getHeight2()+"[String]");
		System.out.println(po1.getBirthday()+"[Date]    "+vo1.getBirthday2()+"[String]");
		
		///*
		//示例二：UserVO -> UserPO
		UserVO vo2 = new UserVO();
		vo2.setId2(null);
		vo2.setName2(123);
		vo2.setAge2("20");
		vo2.setHeight2("170.5F");
		vo2.setBirthday2("2010-10-10");//String -> Date：需要配置XML
		
		UserPO po2 = mapper.map(vo2, UserPO.class);

		System.out.println("\n示例二：由VO向PO转换");
		System.out.println("UserVO	->	UserPO");
		System.out.println(vo2.getId2()+"[Integer]    "+po2.getId()+"[int]");
		System.out.println(vo2.getName2()+"[int]    "+po2.getName()+"[String]");
		System.out.println(vo2.getAge2()+"[String]    "+po2.getAge()+"[Integer] ");
		System.out.println(vo2.getHeight2()+"[String]    "+po2.getHeight()+"[float]");
		System.out.println(vo2.getBirthday2()+"[String]    "+po2.getBirthday()+"[Date]");
		//*/
	}
}
